package com.yzzrjy.Service;

import com.qiniu.util.StringUtils;
import com.yzzrjy.Dao.Impl.BlogDaoMsql;
import com.yzzrjy.Dao.Impl.MemoryDaoRedis;
import com.yzzrjy.Dao.Impl.UserDaoMsql;
import com.yzzrjy.Dao.Interfaces.BlogDao;
import com.yzzrjy.Dao.Interfaces.MemoryDao;
import com.yzzrjy.Dao.Interfaces.UserDao;
import com.yzzrjy.bean.BlogCommentBean;
import com.yzzrjy.bean.BlogInfoBean;
import com.yzzrjy.bean.UserLiveBean;
import com.yzzrjy.data.QiNiu;
import com.yzzrjy.util.IDGenerator;
import com.yzzrjy.util.MyDateUtil;

import java.sql.SQLException;
import java.util.List;

public class BlogService {
    public final static byte PICTURE_TYPE = 1;
    public final static byte VIDEO_TYPE = 2;
    public final static int SHOW_PAGE_SIZE = 20;

    private BlogDao blogDao = new BlogDaoMsql();
    private UserDao userDao = new UserDaoMsql();
    private MemoryDao memoryDao = new MemoryDaoRedis();

    public void addBlog(String uid, String text, String content, byte type, String city) throws SQLException {
        blogDao.addBlog(uid, text, content, type, city);
    }

    public void addBlogWithMemory(String uid, String text, String content, byte type, String city) throws SQLException {
        UserLiveBean bean = userDao.getUserInfoTable(uid);
        if(bean == null){
            throw new RuntimeException("用户为空");
        }
        long newID = IDGenerator.getInstance().nextId();
        memoryDao.addBlog(newID, bean.getSex());
        blogDao.addBlog(newID, uid, text, content, type, city);
    }

    public List<BlogInfoBean> getBlogList(String myUid, String sex, int page) throws SQLException {
        int start = (page - 1) * SHOW_PAGE_SIZE;
        List<BlogInfoBean> list = blogDao.getBlogList(myUid, sex, start, SHOW_PAGE_SIZE);
        fillBlogList(list);
        return list;
    }

    public List<BlogInfoBean> getBlogListByMemory(String myUid, String sex, int page) throws SQLException {
        int start = (page - 1) * SHOW_PAGE_SIZE;
        List<String> blogIdList = memoryDao.getBlogList(sex, start, SHOW_PAGE_SIZE);
        if(blogIdList == null || blogIdList.size() == 0){
            return null;
        }
        String blogIds = StringUtils.join(blogIdList, ",");
        List<BlogInfoBean> beanList = blogDao.getBlogList(myUid, blogIds);
        fillBlogList(beanList);
        return beanList;
    }

    private void fillBlogList(List<BlogInfoBean> list){
        if(list == null || list.size() == 0){
            return;
        }
        for (BlogInfoBean blogBean : list) {
            int type = blogBean.getType();
            String face = blogBean.getFace();
            String birthday = blogBean.getBirthday();
            String content = blogBean.getContent();
            //用户信息
            blogBean.setFace(QiNiu.getSmallImageUrl(face));
            int age = MyDateUtil.getAgeFromBirthday(birthday);
            if (age > 0) {
                blogBean.setAge(String.valueOf(age));
            }
            //图片连接
            if(type == PICTURE_TYPE && content != null){
                blogBean.setPicture_list(ServiceUtils.originJsonToAlbumList(content));
            }else if(type == VIDEO_TYPE && content != null){
                blogBean.setVideo(QiNiu.getOriginalFileUrl(content));
            }
        }
    }

    public void updateBlogLiked(String blogId, String likedUid, boolean liked) throws SQLException {
        blogDao.updateBlogLiked(blogId, likedUid, liked);
    }

    public void addComment(String blogId, String uid, String parentUid, String content) throws SQLException {
        blogDao.addComment(blogId, uid, parentUid, content);
    }

    public List<BlogCommentBean> getBlogComment(String blogId) throws SQLException {
        List<BlogCommentBean> list = blogDao.getBlogComment(blogId);
        if(list == null){
            return null;
        }
        for (BlogCommentBean blogCommentBean : list) {
            blogCommentBean.setFace(QiNiu.getSmallImageUrl(blogCommentBean.getFace()));
        }
        return list;
    }
}
